﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Linq;
using System.Windows.Input;
using Ascend;

namespace AscendViewer
{
	public class ContentExplorerViewModel : ObservableObject
	{
		#region Lifecycle
		public ContentExplorerViewModel(MainWindowViewModel mainWindow)
		{
			MainWindow = mainWindow;

			ExpandAllCommand = new InterfaceCommand(() =>
				{
					if (RootNode != null)
					{
						foreach (ContentNodeViewModel node in RootNode.SelfAndDescendants())
							node.IsExpanded = true;
					}
				}, CanExpandOrCollapseAll);

			CollapseAllCommand = new InterfaceCommand(() =>
				{
					if (RootNode != null)
					{
						foreach (ContentNodeViewModel node in RootNode.SelfAndDescendants())
							node.IsExpanded = false;
					}
				}, CanExpandOrCollapseAll);
		}
		#endregion

		#region Public Properties
		public ICommand ExpandAllCommand
		{
			get;
			private set;
		}

		public ICommand CollapseAllCommand
		{
			get;
			private set;
		}

		public MainWindowViewModel MainWindow
		{
			get;
			private set;
		}

		private ContentNodeViewModel _rootNode;
		public ContentNodeViewModel RootNode
		{
			get
			{
				return _rootNode;
			}

			set
			{
				if (_rootNode == value)
				{
					return;
				}

				// Clear any current selection
				SelectedContentNode = null;

				_rootNode = value;
				NotifyPropertyChanged("RootNode");
			}
		}

		private ContentNodeViewModel _selectedContentNode;
		public ContentNodeViewModel SelectedContentNode
		{
			get
			{
				return _selectedContentNode;
			}

			set
			{
				if (_selectedContentNode == value)
				{
					return;
				}

				// Clear old selection
				if (_selectedContentNode != null)
				{
					_selectedContentNode.IsSelected = false;
				}

				// Update the active armature and animation context
				UpdateActiveArmature(value);
				UpdateAnimationContext(value);

				// Set new selection
				_selectedContentNode = value;

				if (_selectedContentNode != null)
				{
					_selectedContentNode.IsSelected = true;
				}

				NotifyPropertyChanged("SelectedContentNode");
			}
		}

		private ArmatureContentNodeViewModel _activeArmature;
		public ArmatureContentNodeViewModel ActiveArmature
		{
			get
			{
				return _activeArmature;
			}

			private set
			{
				if (_activeArmature == value)
				{
					return;
				}

				// Deactivate old armature
				if (_activeArmature != null)
				{
					_activeArmature.IsActive = false;
				}

				// Activate new armature
				_activeArmature = value;

				if (_activeArmature != null)
				{
					_activeArmature.IsActive = true;
				}
			}
		}

		private AnimatedContentNodeViewModel _animationContext;
		public AnimatedContentNodeViewModel AnimationContext
		{
			get
			{
				return _animationContext;
			}

			set
			{
				if (_animationContext == value)
				{
					return;
				}

				_animationContext = value;
				NotifyPropertyChanged("AnimationContext");
			}
		}

		private bool _isExpanded = false;
		public bool IsExpanded
		{
			get
			{
				return _isExpanded;
			}

			set
			{
				if (_isExpanded == value)
				{
					return;
				}

				_isExpanded = value;
				NotifyPropertyChanged("IsExpanded");
			}
		}

		private string _fileName;
		public string FileName
		{
			get
			{
				return _fileName;
			}
			
			set
			{
				if (_fileName == value)
				{
					return;
				}

				_fileName = value;
				NotifyPropertyChanged("FileName");
			}
		}
		#endregion

		#region Private Methods
		private bool CanExpandOrCollapseAll()
		{
			return RootNode != null && RootNode.SelfAndDescendants().Any(n => n.CanExpand);
		}

		private void UpdateActiveArmature(ContentNodeViewModel node)
		{
			if (node is ArmatureContentNodeViewModel)
			{
				ActiveArmature = (ArmatureContentNodeViewModel)node;
			}
			else if (node is BoneContentNodeViewModel)
			{
				ActiveArmature = ((BoneContentNodeViewModel)node).Armature;
			}
			else
			{
				ActiveArmature = null;
			}
		}

		private void UpdateAnimationContext(ContentNodeViewModel node)
		{
			if (node is AnimatedContentNodeViewModel)
			{
				AnimationContext = (AnimatedContentNodeViewModel)node;
			}
			else if (node is ArmatureContentNodeViewModel)
			{
				AnimationContext =
					(AnimatedContentNodeViewModel)((ArmatureContentNodeViewModel)node).Parent;
			}
			else if (node is BoneContentNodeViewModel)
			{
				AnimationContext =
					(AnimatedContentNodeViewModel)((BoneContentNodeViewModel)node).Armature.Parent;
			}
			else
			{
				AnimationContext = null;
			}
		}
		#endregion
	}
}
